
local director = cc.Director:getInstance()
local TAG_guideRootNode = 2100 --  引导界面的  根节点的tag


local GuideUtil = class("GuideUtil")


--构造函数
function GuideUtil:ctor()
    self._flag_guiding = false  -- 状态标志位  是否正在引导中
    
    
    

end

function GuideUtil:isGuiding()
    return self._flag_guiding 
end
function GuideUtil:setFlag_guiding(flag_guiding)
    self._flag_guiding = flag_guiding
end

function GuideUtil:remove_guide_view() --移除引导界面
    local runningScene= director:getRunningScene()
    runningScene:removeChildByTag(TAG_guideRootNode,true) 
end



--[[
@expectTouchNode  需要被引导的需要玩家点击的node ，如果是空则直接返回
@callback  玩家点击了引导的node 后的 回调事件，可以为空
注意：回调函数会在expectTouchNode 接受的点击事件之前执行 ，请做相应的处理
]]
function GuideUtil:show_guide_view(expectTouchNode,callback)  --显示引导界面
    local runningScene= director:getRunningScene()
    self:remove_guide_view()--清除上次的引导view,每次都重新创建 
    if not expectTouchNode then--希望被点击的那个node
        error("没有expectTouchNode")
        return 
    end
    
    local guideRootNode= display.newNode() --引导界面的根节点
        :addTo(runningScene,TAG_guideRootNode,TAG_guideRootNode) 
    
    --位置及大小信息begin
    local aimSize = expectTouchNode:getCascadeBoundingBox()
    local aimWidth, aimHeight = aimSize.width, aimSize.height
    local anchorPos = expectTouchNode:getAnchorPoint()
    local anchorX, anchorY = anchorPos.x, anchorPos.y
    local aimPos = ConvertPos_SourceNode_to_AimNode(expectTouchNode,false,guideRootNode,false) --转换坐标
    local aimPosX, aimPosY = aimPos.x, aimPos.y
    --位置及大小信息end
    


    
    -- 屏蔽全屏触摸begin
    local touchSwallowNode = display.newLayer()  --
        :align(display.LEFT_BOTTOM, aimPosX - anchorX*aimWidth, aimPosY + aimHeight - anchorY*aimHeight)
        :size(display.width,display.height)
        :addTo(guideRootNode)
    touchSwallowNode:setTouchEnabled(true)
    touchSwallowNode:setTouchSwallowEnabled(true)
    touchSwallowNode:ignoreAnchorPointForPosition(false)
    
    local touchSwallowNode = display.newLayer()  --
        :align(display.LEFT_TOP,aimPosX + aimWidth - anchorX*aimWidth, aimPosY + aimHeight - anchorY*aimHeight)
        :size(display.width,display.height)
        :addTo(guideRootNode)
    touchSwallowNode:setTouchEnabled(true)
    touchSwallowNode:setTouchSwallowEnabled(true)
    touchSwallowNode:ignoreAnchorPointForPosition(false)
    
    local touchSwallowNode = display.newLayer()  --
        :align(display.RIGHT_TOP,aimPosX + aimWidth - anchorX*aimWidth, aimPosY - anchorY*aimHeight)
        :size(display.width,display.height)
        :addTo(guideRootNode)
    touchSwallowNode:setTouchEnabled(true)
    touchSwallowNode:setTouchSwallowEnabled(true)
    touchSwallowNode:ignoreAnchorPointForPosition(false)
    
    
    local touchSwallowNode = display.newLayer() --
        :align(display.RIGHT_BOTTOM,aimPosX - anchorX*aimWidth, aimPosY - anchorY*aimHeight)
        :size(display.width,display.height)
        :addTo(guideRootNode)
    touchSwallowNode:setTouchEnabled(true)
    touchSwallowNode:setTouchSwallowEnabled(true)
    touchSwallowNode:ignoreAnchorPointForPosition(false)
    -- 屏蔽全屏触摸  end
    
    
    
    --添加全屏阴暗遮罩
    local shade_bg = display.newScale9Sprite("shade_background.png", display.cx,display.cy, cc.size(display.width+10, display.height+10))
    shade_bg:setOpacity(140) -- 设置透明度
    
    local stencil = display.newSprite("guide_background.png")--clippingNode 的模板 
    local highLightNode = stencil   -- 也就是高亮区域
    highLightNode:pos(aimPosX,aimPosY)
    local oriSize = highLightNode:getContentSize()
    highLightNode:setScale(aimWidth/oriSize.width, aimHeight/oriSize.height)
    highLightNode:setAnchorPoint(cc.p(anchorX, anchorY))
    
    
    --裁剪节点
    local clipNode = cc.ClippingNode:create()
    clipNode:setAnchorPoint(cc.p(0.5, 0.5))
    clipNode:pos(0,0)
    clipNode:setInverted(true)
    clipNode:setAlphaThreshold(0.5)
    clipNode:addTo(guideRootNode)
    clipNode:setStencil(stencil)
    clipNode:addChild(shade_bg)
    
    
    --捕获引导的点击事件的 节点
    local clickNode = display.newLayer()  --
        :pos(aimPosX, aimPosY)
        :size(aimWidth, aimHeight)
        :addTo(guideRootNode)
    clickNode:setAnchorPoint(cc.p(anchorX, anchorY))
    clickNode:setTouchEnabled(true)
    clickNode:setTouchSwallowEnabled(false)
    clickNode:addNodeEventListener(cc.NODE_TOUCH_EVENT, function(event)
        if event.name == "began" then
            return true
        elseif event.name == "ended" then
            print('clickNodeTouched')
            self:remove_guide_view()--清除上次的引导view
            if type(callback)=="function" then--执行回调函数
            	callback()
            end
        end
    end) --  点击了 引导的按钮 的回调事件
    
    
    
end


function GuideUtil:getExpectTouchNode()  --得到希望点击的那个node 
    return ExpectTouchNode
end







return GuideUtil